from typing import Dict, Any
from src.knowledge.rag import RAGKnowledgeBase
from config import config
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class RAGTool:
    """
    RAG Tool wrapper that can be used by different agents.
    This provides a standardized interface for RAG functionality.
    """
    
    def __init__(self):
        self.rag_kb = RAGKnowledgeBase()
        self.enabled = config.rag_enabled
        logger.info(f"RAGTool initialized. Enabled: {self.enabled}")
        logger.info(f"Knowledge base path: {config.knowledge_base_path}")
    
    def execute(self, query: str) -> Dict[str, Any]:
        """
        Execute a RAG query and return structured results.
        
        Args:
            query (str): The query to send to the knowledge base
            
        Returns:
            Dict[str, Any]: Results from the RAG system
        """
        logger.info(f"Executing RAG query: {query}")
        
        if not self.enabled:
            logger.warning("RAG is disabled")
            return {
                "success": False,
                "result": "RAG is disabled",
                "tool": "rag"
            }
        
        try:
            logger.info("Querying knowledge base")
            result = self.rag_kb.query(query)
            logger.info("RAG query executed successfully")
            return {
                "success": True,
                "result": result,
                "tool": "rag"
            }
        except Exception as e:
            logger.error(f"Error querying RAG: {str(e)}")
            return {
                "success": False,
                "result": f"Error querying RAG: {str(e)}",
                "tool": "rag"
            }